package com.huitone.gddw.controller.analysis;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.common.excel.CreateExcel;
import com.huitone.gddw.controller.analysis.traffic.UpSmRecController;
import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.service.analysis.ISettlementRepService;
import com.huitone.gddw.service.dto.analysis.SettlementRepACDTO;
import com.huitone.gddw.service.dto.analysis.SettlementRepDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 结算报表
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="settlementRep")
public class SettlementRepController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(UpSmRecController.class);
	
	@Autowired
	private ISettlementRepService settlementRepService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public Map<String, Object> query(HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询结算报表";
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SettlementRepACDTO re = getList(request);
			result.put("data", re);
		} catch (Exception e) {
			LOGGER.error("查询结算报表出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 导出 Excel
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="/out-excel", method=RequestMethod.POST)
	public void outExcel(HttpServletRequest request, HttpServletResponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "结算报表";
		
		Map<String, Object> params = new HashMap<String, Object>();
		for (Object name : request.getParameterMap().keySet()) {
			params.put(name.toString(), request.getParameter(name.toString()));
		}
		
		SettlementRepACDTO re = getList(request);
		if (re != null && (null == re.getRecordList() || 0 == re.getRecordList().size())) {
			re.getRecordList().add(new SettlementRepDTO());
		}
		
		HSSFWorkbook wb = (HSSFWorkbook)CreateExcel
				.getInstance()
				.CreateNewExcelNoTemplate(fileName, re.getRecordList(), re).getWorkbook();
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename="+fileName+sdf.format(new Date())+".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}
	
	/**
	 * 获取列表数据
	 * @param request
	 * @return SettlementRepACDTO
	 */
	 public SettlementRepACDTO getList(HttpServletRequest request) {
		Map<String, Object> params = getParams(request);
		return settlementRepService.findCount(params);
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String dateFormat = request.getParameter("dateFormat"); // year年 month月
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dateFormat", dateFormat);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		return params;
	}
	
}
